<?php

use Illuminate\Database\Capsule\Manager as DB;
#[用户反馈管理]
class FeedbackController extends AcommonController
{
    public function init() {
        $this->model = new feedbackModel();
        $this->table = 'feedback';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[用户反馈列表]
    public function indexAction($returnData = FALSE)
    {

        $this->model = $this->model->with(['member', 'oper']);
        if($this->isPost()){
            $post_data = $this->postData;
            $search_data = [];
            if(isset($post_data['mode'])){
                switch ($post_data['mode']){
                    case 'handled':
                        array_push($search_data, ['status', '=', 1]);
                        break;
                    case 'unhandle':
                        array_push($search_data, ['status', '=', 0]);
                        break;
                }
            }
            if(isset($post_data['type']) && in_array($post_data['type'], array_keys(feedbackModel::getAllType()))){
                array_push($search_data, ['type', '=', $post_data['type']]);
            }
            if(isset($post_data['start_on']) && !empty($post_data['start_on'])){
                $this->model = $this->model->whereDate('created_at', '>=', $post_data['start_on']);
            }
            if(isset($post_data['end_on']) && !empty($post_data['end_on'])){
                $this->model = $this->model->whereDate('created_at', '<=', $post_data['end_on']);
            }
            $keywordsTypeArray = [
                'user_id' => 'user_id',
                'user_phone' => 'user_phone'
            ];
            if(isset($post_data['keywordsType']) && isset($post_data['keywords'])
                && in_array($post_data['keywordsType'], $keywordsTypeArray)
                && !empty($post_data['keywords'])){
                if($post_data['keywordsType'] == $keywordsTypeArray['user_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($search_data, ['user_id', '=', $post_data['keywords']]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_phone']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($this->condition_in, ['user_id', memberModel::query()->where('phone', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray()]);
                    }
                }
            }
            unset($keywordsTypeArray);
            $this->search = $search_data;
            return parent::indexAction(false);
        }else{
            $this->_view->assign('all_type', json_encode(feedbackModel::getAllType(), JSON_UNESCAPED_UNICODE));
            return parent::indexAction($returnData);
        }
    }

    #[反馈处理]
    public function handleAction()
    {
        if(!$this->isPost()){
            $id = $this->get('id');
            try {
                $feedback = feedbackModel::query()->with(['member', 'oper'])->findOrFail($id)->toArray();
                $feedback['images'] = explode(',', $feedback['images']);
                $this->_view->assign('dataset', json_encode($feedback, JSON_UNESCAPED_UNICODE));
                $this->_view->display( strtolower($this->controller) . '/info.html');
            }catch (Exception $e){
                throw new Exception('反馈信息不存在！');
            }
        }else{
            $opt_id = $this->postData['id'];
            try {
                $remark = $this->get('remark');
                if(empty($remark)){
                    throw new Exception('处理意见不能为空');
                }
                if(mb_strlen($remark) > 200){
                    throw new Exception('处理意见不能超过200个字');
                }
                $feedbackModel = $this->model->where([[$this->primaryKey, $opt_id],['status', 0]])->first();
                if(!$feedbackModel){
                    throw new Exception('反馈不存在或已处理！');
                }
                $res = DB::transaction(function () use ($feedbackModel, $remark) {
                    $feedbackModel->status = 1;
                    $feedbackModel->remark = $remark;
                    $feedbackModel->oper_id = $this->auth->user_id;
                    $feedbackModel->updated_at = timestamp();
                    return $feedbackModel->save();
                });
                if($res){
                    ret(0, '恭喜，反馈处理成功！');
                }else{
                    throw new Exception('抱歉，反馈操作失败！');
                }
            }catch (Exception $e){
                ret(1, $e->getMessage());
            } finally {
                unset($opt_id);
            }
        }
    }

    #[删除反馈]
    public function deleteAction()
    {
        parent::deleteAction(); // TODO: Change the autogenerated stub
    }
}